﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace BUL
{
   
   public class Advertise_Business
    {
       readonly CTAT8_VNEntities _dal = new CTAT8_VNEntities();

        public void GetAllAdvertise(DataGridView dataGridView)
        {
            if(dataGridView.Rows.Count>0)
                dataGridView.Rows.Clear();
            var j = 0;
            var result = _dal.Advertises.OrderBy(x => x.Name).Select(x=> new {Id=x.Id, Name = x.Name, Link= x.Link, Possition = x.Position, Order = x.Ord, Content = x.Content }).ToList();
            foreach (var item in result)
            {
                dataGridView.Rows.Add();
                dataGridView[0, j].Value = j+1;
                dataGridView[1, j].Value = item.Id;
                dataGridView[2, j].Value = item.Name;
                dataGridView[3, j].Value = item.Link;
                dataGridView[4, j].Value = item.Possition;
                dataGridView[5, j].Value = item.Order;
                dataGridView[6, j].Value = item.Content;

                j++;
            }
        }

       public void InsertAdvertise(string name, string link, string possition, string order, string content)
       {
           var addItem = new Advertise
                             {
                                 Name=name,
                                 Link=link,
                                 Position = Convert.ToInt16(possition),
                                 Ord = Convert.ToInt32(order),
                                 Content = content
                             };
           _dal.Advertises.AddObject(addItem);

           _dal.SaveChanges();
       }

       public void UpdateAdvertise(int id,string name, string link, string possition, string order, string content)
       {
           var updateItem = _dal.Advertises.Where(x => x.Id == id).FirstOrDefault();
            if(updateItem==null)
               return;
           updateItem.Name = name;
           updateItem.Link = link;
           updateItem.Position =Convert.ToInt16(possition);
           updateItem.Ord = Convert.ToInt32(order);
           updateItem.Content = content;

           _dal.SaveChanges();
       }

       public void DeleteAdvertise(int id)
       {
           var deleteItem = _dal.Advertises.Where(x => x.Id == id).FirstOrDefault();
           if(deleteItem==null)
               return;

           _dal.Advertises.DeleteObject(deleteItem);

           _dal.SaveChanges();
       }

       public string ValidationData(string name, string possition, string ord)
       {
           if (string.IsNullOrEmpty(name))
               return "N";
           
           if(possition!=null)
           {
               int possotion=0;
               var isInteger = int.TryParse(possition,out possotion);
               if (!isInteger)
                   return "P";
           }

           int ordOut = 0;
           var isOrder = int.TryParse(ord, out ordOut);
           if (!isOrder)
               return "O";
           
           return "";
       }

    }
}
